package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysUserPostMapper;
import org.dromara.system.pojo.bo.SysUserPostBO;
import org.dromara.system.pojo.entity.SysUserPostDO;
import org.dromara.system.pojo.qo.SysUserPostQO;
import org.dromara.system.pojo.vo.SysUserPostVO;
import org.dromara.system.service.SysUserPostService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户与岗位关联表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysUserPostServiceImpl implements SysUserPostService {

    private final SysUserPostMapper sysUserPostMapper;

    @Override
    public boolean add(SysUserPostBO sysUserPostBO) {
        SysUserPostDO sysUserPostDO = BeanCopyUtils.copy(sysUserPostBO, SysUserPostDO.class);
        return sysUserPostMapper.insert(sysUserPostDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysUserPostMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysUserPostBO sysUserPostBO) {
        SysUserPostDO sysUserPostDO = BeanCopyUtils.copy(sysUserPostBO, SysUserPostDO.class);
        return sysUserPostMapper.update(sysUserPostDO) > 0;
    }

    @Override
    public List<SysUserPostVO> list(SysUserPostQO sysUserPostQO) {
        SysUserPostDO sysUserPostDO = BeanCopyUtils.copy(sysUserPostQO, SysUserPostDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysUserPostDO);
        return sysUserPostMapper.selectListByQueryAs(queryWrapper, SysUserPostVO.class);
    }

    @Override
    public SysUserPostVO getInfo(Long id) {
        SysUserPostDO sysUserPostDO = sysUserPostMapper.selectOneById(id);
        if (sysUserPostDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysUserPostDO, SysUserPostVO.class);
    }

    @Override
    public Page<SysUserPostVO> page(SysUserPostQO page) {
        return sysUserPostMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysUserPostVO.class);
    }

}

